package com.mobi.cacti.ns.view;

import java.util.ArrayList;

import org.holoeverywhere.LayoutInflater;
import org.holoeverywhere.app.Fragment;
import org.holoeverywhere.widget.ListView;
import org.holoeverywhere.widget.ProgressBar;
import org.holoeverywhere.widget.TextView;

import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.mobi.cacti.R;
import com.mobi.cacti.ns.model.ListItem;
import com.mobi.cacti.ns.view.adapter.ItemListAdapter;
import com.mobi.cacti.ns.view.tab.BaseContainerFragment;
import com.mobi.cacti.rabbit.controllers.MainInfoActivityController;
import com.mobi.cacti.rabbit.controllers.MainInfoActivityControllerCallback;
import com.mobi.cacti.rabbit.response.Cache;
import com.mobi.cacti.rabbit.response.CustomerInfoCurcitData;

public class CustServiceInfoActivity extends Fragment implements
		MainInfoActivityControllerCallback {

	private MainInfoActivityController mainInfoActivityController;
	private TextView customerContactTextView;
	private TextView companyNameTextView;
	private TextView contactPersonNameTextView;
	private TextView emailTextView;
	private TextView phoneTextView;
	private ListView custServiceInfoListView;

	private ProgressBar progressBar;
	
	private ItemListAdapter adapter;
	
	private ArrayList<ListItem> items;
	private CustomerInfoCurcitData[] customerInfoCircuitDataArray;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mainInfoActivityController = MainInfoActivityController.getInstance();
		mainInfoActivityController.delegate = this;
		
		items = new ArrayList<ListItem>();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		mainInfoActivityController.loadCustomerServiceInfo(this.getActivity());
		
		View v = LayoutInflater.from(getActivity()).inflate(
				R.layout.custserviceinfo_layout, null);
		
		progressBar = (ProgressBar) v.findViewById(R.id.progressbar);
		
		customerContactTextView = (TextView) v.findViewById(R.id.customercontacttextview);
		companyNameTextView = (TextView) v.findViewById(R.id.companynametextview);
		contactPersonNameTextView = (TextView) v.findViewById(R.id.contactpersonnametextview);
		emailTextView = (TextView) v.findViewById(R.id.emailtextview);
		phoneTextView = (TextView) v.findViewById(R.id.phonetextview);
		
		items.clear();
		customerContactTextView.setText("Customer Contact: ");
		companyNameTextView.setText("Company Name: ");
		contactPersonNameTextView.setText(": ");
		
		// MOve contactPersonNameTextView to align with upper text view
		Rect bounds = new Rect();
		Paint textPaint = companyNameTextView.getPaint();
		textPaint.getTextBounds("Company Name",0,"Company Name".length(),bounds);
		int width = bounds.width();
		contactPersonNameTextView.setPadding(width+2,
				contactPersonNameTextView.getPaddingTop(), 
				contactPersonNameTextView.getPaddingRight(), 
				contactPersonNameTextView.getPaddingBottom());
		
		emailTextView.setText("Email: ");
		phoneTextView.setText("Phone: ");
		
		adapter = new ItemListAdapter(getSupportActivity(), R.layout.rowitem_layout, items);
		custServiceInfoListView = (ListView) v.findViewById(R.id.custserviceinfolist);
		custServiceInfoListView.setAdapter(adapter);
		
		custServiceInfoListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				
				CustomerInfoCurcitData data = customerInfoCircuitDataArray[position];
				
				Bundle bundle = new Bundle();
				bundle.putParcelable("data", data);
				
				CustInfoLinkInformationActivity custInfoLinkInformationActivity = new CustInfoLinkInformationActivity();
				custInfoLinkInformationActivity.setArguments(bundle);
				
				((BaseContainerFragment)getParentFragment()).replaceFragment(custInfoLinkInformationActivity, true);
			}
		});
		
		return v;
	}

	@Override
	public void mainInfoActivityControllerCompleteLoadCustomerServiceInfo() {
		
		progressBar.setVisibility(View.GONE);
		
		items.clear();

		String companyName = "Company Name: " + Cache.reqW4CusInfo.ComNameE;
		String contactPersonName = ": " + Cache.reqW4CusInfo.ComNameT;
		String contactEmail = "Email: " + Cache.reqW4CusInfo.ComEmail;
		String contactTel = "Phone: " + Cache.reqW4CusInfo.ComTel;
		
		companyNameTextView.setText(companyName);
		contactPersonNameTextView.setText(contactPersonName);
		emailTextView.setText(contactEmail);
		phoneTextView.setText(contactTel);
		
		customerInfoCircuitDataArray = Cache.reqW4CusInfo.getArrayOfCustomerInforCurcitData();
				
		for (CustomerInfoCurcitData data : customerInfoCircuitDataArray) {
			ListItem item = new ListItem();
			item.label = "Circuit No. " + data.id;
			item.ticketCountLabel = 	null;
			item.isShowRightArrow = true;
			items.add(item);
		}
		
		adapter.notifyDataSetChanged();
		
		LayoutParams lp = (LayoutParams) custServiceInfoListView.getLayoutParams();
		int height = customerInfoCircuitDataArray.length * 109;
		
		// customerInfoCircuitDataArray list is in which all data is kept
		lp.height = height;
		custServiceInfoListView.setLayoutParams(lp);
	}

}
